<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
class Erp extends Base {
    /**
     * 同步学校数据(一天一次)
     */
    public function schoolErp()
    {
        extract(input());
//        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $in_up = [
            'school_name' => $school_name,
            'school_name_remark' => $school_name,
            'school_address' => $school_sheng . $school_shi . $school_qu,
            'school_time' => $school_time,
            'school_update_time' => $school_update_time,
            'school_erp' => $school_erp,
            'school_head' => $school_head,
            'school_shui' => $school_shui,
            'school_type' => $CusType
        ];
        $CusType = explode(',',$CusType);
        if(in_array('幼儿园',$CusType)) $in_up['school_type'] = 1;
        if(in_array('小学',$CusType)) $in_up['school_type'] = 2;
        if(in_array('中学',$CusType) || in_array('初中',$CusType) || in_array('高中',$CusType) || in_array('中专',$CusType) || in_array('大专',$CusType) || in_array('大学',$CusType)) $in_up['school_type'] = 3;
        if($dEndDate) $in_up['school_status'] = 2;
        if($school_sheng){
            $in_up['school_sheng'] = Db::table('up_area')
                ->where('district',$school_sheng)->where('level',1)->value('district_id');
            if($in_up['school_sheng'] && $school_shi){
                $in_up['school_shi'] = Db::table('up_area')
                    ->where('district',$school_shi)->where('pid',$in_up['school_sheng'])->where('level',2)
                    ->value('district_id');
                if($in_up['school_shi'] && $school_qu){
                    $in_up['school_qu'] = Db::table('up_area')
                        ->where('district',$school_qu)->where('pid',$in_up['school_shi'])->where('level',3)
                        ->value('district_id');
                }
            }
        }
        $have = Db::table('up_school')->where('school_erp',$school_erp)->find();
        if($have){
            $in_up['school_update_time'] = $today;
            $res = Db::table('up_school')->where('school_erp',$school_erp)->update($in_up);
        }else{
            $res = Db::table('up_school')->insert($in_up);
            //-------------------------------------------------------------------------------------------
            $insert = [];
            if(in_array('幼儿园',$CusType)){
                $insert = [
                    [
                        'grade_name' => '小班',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 1,
                    ],[
                        'grade_name' => '中班',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 2,
                    ],[
                        'grade_name' => '大班',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 3,
                    ]
                ];
            };
            if(in_array('小学',$CusType)) {
                $insert = [
                    [
                        'grade_name' => '一年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 4,
                    ],[
                        'grade_name' => '二年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 5,
                    ],[
                        'grade_name' => '三年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 6,
                    ],[
                        'grade_name' => '四年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 7,
                    ],[
                        'grade_name' => '五年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 8,
                    ],[
                        'grade_name' => '六年级',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 9,
                    ]
                ];
            };
            if(in_array('中学',$CusType) || in_array('初中',$CusType)){
                $insert = [
                    [
                        'grade_name' => '初一',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 10,
                    ],[
                        'grade_name' => '初二',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 11,
                    ],[
                        'grade_name' => '初三',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 12,
                    ]
                ];
            };

            if(in_array('高中',$CusType) || in_array('中专',$CusType) || in_array('大专',$CusType) || in_array('大学',$CusType)){
                $insert = [
                    [
                        'grade_name' => '高一',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 13,
                    ],[
                        'grade_name' => '高二',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 14,
                    ],[
                        'grade_name' => '高三',
                        'grade_school_erp' => $school_erp,
                        'grade_time' => $today,
                        'grade_update_time' => $today,
                        'grade_level' => 15,
                    ]
                ];
            };
            if($insert){
                Db::table('up_school_grade')->insertAll($insert);
            }

            //-------------------------------------------------------------------------------------------
        }
        $return = [
            'errcode' => 0,
            'errmsg' => '',
            'id' => '',
            'code' => '',
            'tradeid' => '',
            'data' => '',
        ];
        if(!$res){
            $return['errcode'] = -1;
            $return['errmsg'] = $school_erp;
        }
        return json($return);
    }
    /**
     * 同步学校对应年级(一天一次，晚于学校)
     */
    public function schoolGradeErp()
    {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $field = [
            'ID', 'SCHOOL_ERP','GRADE'
        ];
        $data = $this->getErpData('YZ_SH_CLASS',$field);
//        dump($data);die;
        if($data[0]['rows']){
            $level = [
                '小班' => 1, '中班' => 2,'大班' => 3,
                '一年级' => 4, '二年级' => 5, '三年级' => 6, '四年级' => 7, '五年级' => 8, '六年级' => 9,
                '初一' => 10, '初二' => 11, '初三' => 12,
                '高一' => 13, '高二' => 14, '高三' => 15,
                '中专一' => 16, '中专二' => 17, '中专三' => 18,
                '大一' => 19, '大二' => 20, '大三' => 21,
            ];
            foreach ($data[0]['rows'] as $k => $v){
                $sort = 1;
                if($v[2] != '小班'){
                    if(isset($level[$v[2]]) && $level[$v[2]]){
                        $sort = $level[$v[2]];
                    }
                }
                $in_up = [
                    'grade_name' => $v[2],
                    'grade_school_erp' => $v[1],
                    'grade_level' => $sort,
                    'grade_time' => $today,
                    'grade_update_time' => $today
                ];
                $have = Db::table('up_school_grade')
                    ->where('grade_school_erp',$v[1])
                    ->where('grade_name',$v[2])
                    ->find();
                if(!$have){
                    $res = Db::table('up_school_grade')->insert($in_up);
                }else{
                    $res = true;
                }
                if($res){
                    $this->updateErpData('YZ_SH_CLASS',$v[0]);
                }
            }
        }
        exit();
    }
    /**
     * 同步校服数据(一天一次)
     */
    public function goodsErp()
    {
        extract(input());
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $sex = 0;
        if($good_sex == '男款') $sex = 1;
        if($good_sex == '女款') $sex = 2;
        if($good_sex == '通用') $sex = 3;
        $in_up = [
            'good_name' => $good_name,
            'good_price' => $good_price,
            'good_create_time' => $good_create_time,
            'good_update_time' => $good_update_time,
            'good_stock' => 100,
            'good_coding' => $good_coding,
            'good_coding_less' => substr($good_coding,1,12),
            'good_color' => $good_color,
            'good_pinming' => $good_pinming,
            'good_type' => $good_type,
            'good_sex' => $sex,
            'good_q' => 1
        ];
        if($dEndDate){
            $in_up['good_status'] = 2;
        }
        $have = Db::table('up_goods')->where('good_coding',$good_coding)->field('good_id')->find();
        $specs = json_decode(json_encode($csizeList),true);
        $return = [
            'errcode' => 0,
            'errmsg' => '',
            'id' => '',
            'code' => '',
            'tradeid' => '',
            'data' => '',
        ];

        Db::startTrans();
        try{
            if($have){
                if($specs){
                    $inserts = [];
                    foreach ($specs as $k => $v){
                        $spec_id = Db::table('up_good_spec')
                            ->where('spec_good_coding',$good_coding)
                            ->where('spec_size',$v['csize'])
                            ->value('spec_id');
                        if(empty($spec_id)){
                            $spec = [
                                'spec_good_id' => $have['good_id'],
                                'spec_size' => $v['csize'],
                                'spec_size_two' => $v['csizevalue'],
                                'spec_size_type' => $v['specialbody'],
                                'spec_size_q' => 1,
                                'spec_good_coding' => $good_coding,
                                'spec_good_coding_less' => substr($good_coding,1,12)
                            ];
                            $inserts[] = $spec;
                        }
                    }
                    if($inserts){
                        Db::table('up_good_spec')->insertAll($inserts);
                    }
                }
                unset($in_up['good_create_time']);
                Db::table('up_goods')->where('good_coding',$good_coding)->update($in_up);
            }else{
                $gid = Db::table('up_goods')->insertGetId($in_up);
                if($specs){
                    $inserts = [];
                    foreach ($specs as $k => $v){
                        $spec = [
                            'spec_good_id' => $gid,
                            'spec_size' => $v['csize'],
                            'spec_size_two' => $v['csizevalue'],
                            'spec_size_type' => $v['specialbody'],
                            'spec_size_q' => 1,
                            'spec_good_coding' => $good_coding,
                            'spec_good_coding_less' => substr($good_coding,1,12)
                        ];
                        $inserts[] = $spec;
                    }
                    Db::table('up_good_spec')->insertAll($inserts);
                }
            }
            Db::commit();
            return json($return);
        }catch (\Exception $e) {
            Db::rollback();
            $return['errcode'] = -1;
            $return['errmsg'] = $good_coding;
            return json($return);
        }
    }
    /**
     * 同步销售订单(一天一次)
     */
    public function schoolGoodsErp()
    {
        extract(input());
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        Db::table('up_zzzz_zerp')->insert([
            'content' => json_encode($cusPriceList),
            'time' => $today
        ]);
        $datas = json_decode(json_encode($cusPriceList),true);
        if($datas){
            $errstr = '';
            foreach ($datas as $k => $v){
                if(!$v['sg_year']){
                    $v['sg_year'] = '';
                }
                $where = [
                    'sg_school_erp' => $v['sg_school_erp'],
                    'sg_good_coding' => $v['sg_good_coding'],
                    'sg_year' => $v['sg_year'],
//                    'sg_sign' => $v['sg_sign'],
                    'sg_sex' => $v['sg_sex'],
                ];
                $in_up = [
                    'sg_school_erp' => $v['sg_school_erp'],
                    'sg_good_unicode' => '',
                    'sg_good_coding' => $v['sg_good_coding'],
                    'sg_good_coding_less' => substr($v['sg_good_coding'],1,12),
                    'sg_year' => $v['sg_year'],
                    'sg_sign' => $v['sg_sign'],
                    'sg_sex' => $v['sg_sex'],
                    'sg_time' => $today,
                    'good_price' => $v['good_price'],
                    'sg_updatetime' => $today
                ];
                $have = Db::table('up_school_good')->where($where)->find();
                if(!$have){
                    try{
                        $res = Db::table('up_school_good')->insert($in_up);
                    }catch(\Exception $e){
                        $errstr .= $v['ccode'] . '插入失败,';
                    }

                }else{
                    unset($in_up['sg_time']);
                    if($v['itype'] == 3){
                        $in_up['sg_status'] = 2;
                    }
                    try{
                        $res = Db::table('up_school_good')->where('sg_id',$have['sg_id'])->update($in_up);
                    }catch(\Exception $e){
                        $errstr .= $v['ccode'] . '更新失败,';
                    }
                }
            }
            $return = [
                'errcode' => 0,
                'errmsg' => '',
                'id' => '',
                'code' => '',
                'tradeid' => '',
                'data' => '',
            ];
            if($errstr){
                $return['errcode'] = -1;
                $return['errmsg'] = trim($errstr,',');
            }
            return json($return);
        }
    }
}